package pet_pavilion.yushougeums.controller;

import cn.tedu.yushouge.commons.consts.HttpConsts;
import cn.tedu.yushouge.commons.security.LoginPrincipal;
import cn.tedu.yushouge.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import pet_pavilion.yushougeums.pojo.dto.UserLoginInfoDTO;
import pet_pavilion.yushougeums.pojo.param.UserAddNewParam;
import pet_pavilion.yushougeums.pojo.param.UserLoginInfoParam;
import pet_pavilion.yushougeums.pojo.param.UserUpdateInfoParam;
import pet_pavilion.yushougeums.pojo.vo.UserStandardVO;
import pet_pavilion.yushougeums.service.IUserService;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Api(tags = "01. 用户登录")
@Slf4j
@Validated
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    PasswordEncoder passwordEncoder;

    public UserController(){
        log.debug("创建控制器类对象：AdminController");
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 100)
    public JsonResult<UserLoginInfoDTO> login(UserLoginInfoParam userLoginInfoParam, @ApiIgnore HttpServletRequest request){
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginInfoParam);
        System.out.println("开始处理【用户登录】的请求，参数：{"+ userLoginInfoParam+"}");
        String remoteAddr = request.getRemoteAddr();
        log.warn("111111111:{}"+remoteAddr);
        String userAgent = request.getHeader(HttpConsts.HEADER_USER_AGENT);
        log.warn("22222222222222:{}"+userAgent);
        UserLoginInfoDTO userLogin=userService.login(userLoginInfoParam, remoteAddr, userAgent);
        log.debug("即将响应：{}", userLogin);
        return JsonResult.ok(userLogin);
    }

    @PostMapping("/add-new")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 200)
    public JsonResult addNew( UserAddNewParam userAddNewParam) {
//        System.out.println("/add-new已建立连接");
        log.debug("开始处理【添加用户】的请求，参数：{}", userAddNewParam);
        userService.addNew(userAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出登录")
    @ApiOperationSupport(order = 201)
    public JsonResult<Void> logout(@RequestHeader(HttpConsts.HEADER_AUTHORIZATION) String jwt) {
        log.debug("开始处理【管理员退出登录】的请求，参数：{}", jwt);
        userService.logout(jwt);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改用户详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的用户ID值！") Long id,
                                     @Valid UserUpdateInfoParam userUpdateInfoParam
                                    ) {
        log.debug("开始处理【修改管理员详情】的业务，参数：{}", userUpdateInfoParam);
        userUpdateInfoParam.setPassword(passwordEncoder.encode(userUpdateInfoParam.getPassword()));
        userService.updateById(id, userUpdateInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("enable")
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@Range(min = 1, message = "请提交有效的管理员ID值！") @RequestParam Long id) {
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询用户详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询管理员详情失败，请提交合法的ID值！")
                                      @RequestParam Long id
//                                      ,@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal
    ) {
        log.debug("开始处理【根据ID查询管理员详情】的请求，参数：{}", id);
//        log.debug("当事人ID：{}", loginPrincipal.getId());
//        log.debug("当事人用户名：{}", loginPrincipal.getUsername());
        UserStandardVO result = userService.getStandardById(id);
        return JsonResult.ok(result);
    }


}
